package com.sh.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.druid.util.StringUtils;
import com.sh.dao.RepairDao;
import com.sh.entity.Repair;
import com.sh.entity.User;
import com.sh.service.RepairService;
import com.sh.util.DateUtil;

@Service
public class RepairServiceImpl implements RepairService{
	@Autowired
	private RepairDao repairDao;

	@Override
	public List<Repair> listRepair(Map<String, Object> conMap,User user,String sysTime) {
		// TODO Auto-generated method stub 

		return repairDao.listRepair(conMap);

	}
	//上传 excel文件,并且到DB
	@Override
	public String saveUpload(List<Repair> checkList, User user, String sysTime) {
		// TODO Auto-generated method stub
		repairDao.saveUpload(checkList,user,sysTime);

		return "";
	}
	//查询数量
	@Override
	public int listRepairCount(Map<String, Object> conMap, User user, String sysTime) {
		// TODO Auto-generated method stub
		return repairDao.listRepairCount(conMap);
	}
	/*
	 * 删除
	 * @see com.sh.service.RepairService#deleteRepairs(java.lang.String[])
	 */
	@Override
	public void deleteRepairs(String[] ids) {
		// TODO Auto-generated method stub
		repairDao.deleteRepairs(ids);
	}
	/*
	 * 保存 数据
	 * @see com.sh.service.RepairService#saveRepairs(com.sh.entity.Repair, com.sh.entity.User)
	 */
	@Override
	public void saveRepairs(Repair repair, User user) {
		// TODO Auto-generated method stub
		repairDao.saveRepairs(repair,user);
	}
	/*
	 * 判断 数据是不是已经存在
	 * 根据 【日期】【车号】【客户名】【金额】判断
	 * @see com.sh.service.RepairService#isRepairExist(com.sh.entity.Repair)
	 */
	@Override
	public boolean isRepairExist(Repair repair) {
		// TODO Auto-generated method stub
		boolean flag = false;
		//修改的时候判断 客户名称有么有存在
		if(!StringUtils.isEmpty(repair.getId())){
			int count = repairDao.isRepairExistForUpdate(repair);
			flag = (count==0?false:true);
		}else{
			int count = repairDao.isRepairExistForAdd(repair);
			flag = (count==0?false:true);
		}

		return flag;
	}

	//根据ID 查询 对应日期
	@Override
	public List<String> listYearByIds(String[] ids) {
		// TODO Auto-generated method stub
		List<String> rtnList = new ArrayList<>();
		List list = repairDao.listYearByIds(ids);
		if(list!=null &&
				list.size()>0){
			for(int i=0;i<list.size();i++){
				Map map = (Map)list.get(i);
				String date =
						(String)map.get("repair_date");
				if(DateUtil.checkDateFormat(date, "yyyy-MM-dd")){
					if(!rtnList.contains(date.split("-")[0])){
						rtnList.add(date.split("-")[0]);
					}
				}
			}
			return rtnList;

		}
		return null;
	}




}
